package com.example.water.controller;

import com.example.water.pojo.RespBean;
import com.example.water.pojo.StSitinfoB;
import com.example.water.pojoDTO.StSitinfoBDTO;
import com.example.water.service.StRiverRService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author Shunrai
 * @create 2023/11/9 14:46
 * @Version 1.0
 * @Description
 */
@RestController
@Tag(name = "实时水情-河流")
@RequestMapping("/RiverR")
@ApiSupport(author = "shunrai")
public class StRiverRController {
    @Autowired
    private StRiverRService stRiverRService;

    @Operation(summary = "获取所有河流数据")
    @ApiResponse(responseCode = "404", description = "查询失败")
    @ApiResponse(responseCode = "200", description = "查询成功")
    @GetMapping("/getAll")
    public RespBean<StSitinfoBDTO> getAllRiver() {
        Integer count = stRiverRService.getAllCount();
        List<StSitinfoB> rPage = stRiverRService.getStRiverRPage(count, 1);
        StSitinfoBDTO stSitinfoBDTO = new StSitinfoBDTO(rPage, count);
        return RespBean.success("查询成功", stSitinfoBDTO);
    }

    @Operation(summary = "分页获取河流数据")
    @GetMapping("/page")
    public RespBean<StSitinfoBDTO> getRiverRPage(@RequestParam(defaultValue = "7", required = false) Integer pageSize,
                                                 @RequestParam(defaultValue = "1", required = false) Integer currentPage) {
        List<StSitinfoB> rPage = stRiverRService.getStRiverRPage(pageSize, currentPage);
        Integer count = stRiverRService.getAllCount();
        StSitinfoBDTO stSitinfoBDTO = new StSitinfoBDTO(rPage, count);
        return RespBean.success("查询成功", stSitinfoBDTO);
    }

    @Operation(summary = "河流名搜索")
    @GetMapping("/search")
    public RespBean<StSitinfoBDTO> searchRiver(@Parameter(description = "河流名称", required = true) String rivername,
                                               @RequestParam(defaultValue = "7", required = false) Integer pageSize,
                                               @RequestParam(defaultValue = "1", required = false) Integer currentPage) {
        String stName1 = rivername.trim();
        if (!"".equals(stName1)) {
            //获取所有数据
            Integer count = stRiverRService.getAllCount();
            List<StSitinfoB> rPage = stRiverRService.getStRiverRPage(count, 1);
            List<StSitinfoB> stream = rPage.stream().filter(v -> Objects.equals(v.getRivername(), stName1)).collect(Collectors.toList());
            List<StSitinfoB> resultList;
            if (stream.size() > (currentPage - 1) * pageSize) {
                // 计算结束索引，避免超出列表范围
                int endIndex = Math.min((currentPage - 1) * pageSize + pageSize, stream.size());
                resultList = stream.subList((currentPage - 1) * pageSize, endIndex);
            } else {
                // 如果原始列表不足 2 个元素，则返回空列表
                resultList = Collections.emptyList();
            }
            StSitinfoBDTO stSitinfoBDTO = new StSitinfoBDTO(resultList, stream.size());
            return RespBean.success("搜索成功", stSitinfoBDTO);
        } else {
            return getRiverRPage(7, 1);
        }

    }
}
